package com.itheima.service;

import com.itheima.aop.Log;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    //添加
    @Override
    public void addDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.addDept(dept);

    }

    //删除
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteDept(Integer id) {
        deptMapper.deleteDept(id);

        empMapper.deleteEmp(id);
    }

    //修改
    @Override
//   @ Log 表示当前方法属于目标方法
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.updateDept(dept);

    }

    //查找
    @Override
//    @Log 表示当前方法属于目标方法
    public List<Dept> findAllDept() {
        List<Dept> allDept = deptMapper.findAllDept();
        return allDept;
    }

    //id查找
    @Override
    public Dept findById(Integer id) {
        Dept byId = deptMapper.findById(id);

        return byId;
    }
}
